package com.hackerrank.challenges.ukandus;

import java.util.HashMap;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		in.nextLine();
		HashMap<String, Integer> w = new HashMap<String, Integer>();
		for(int n = 0; n < N; n++) {
			String wa[] = in.nextLine().split("\\s+");
			for(String word : wa) {
				Integer c = w.get(word);
				if(c == null)
					c = 0;
				c++;
				w.put(word, c);
			}
		}
		int T = in.nextInt();
		in.nextLine();
		for(int t = 0; t < T; t++) {
			String f1 = in.nextLine(), f2 = f1.substring(0, f1.length() - 2) + "se";
			Integer c1 = w.get(f1), c2 = w.get(f2);
			if(c1 == null)
				c1 = 0;
			if(c2 == null)
				c2 = 0;
			System.out.println(c1 + c2);
		}
	}
}
